package fileWorker;

import java.io.File;
import java.util.ArrayList;

import javax.naming.AuthenticationException;

import adProvider.ADConnection;
import adProvider.ADQuery;

public class CreateADUsersFromFile {

	public void userCreater() {

		ADConnection adc = new ADConnection();

		adc.useSSL(true);

		adc.setSERVER("S02");
		adc.setROOT("moniki.local");

		adc.setLOGIN("task");
		adc.setPASS("435ZfpCQ");

		try {
			adc.connect();
		} catch (AuthenticationException e) {
			e.printStackTrace();
		}
		ADQuery adq = new ADQuery(adc);

		FileReader fr = new FileReader();
		File file = new File("C:\\pat.txt");

		ArrayList<String> list = fr.fileToList(file, "cp1251");

		for (int i = 0; i < list.size(); i++) {
			String[] fio = list.get(i).split(" ");

			String userDN = adq.createADUser(fio[0], fio[1], fio[2],
					"OU=NEW,OU=MONIKI Users", false);

			if (userDN != null) {
				adq.changeUserPassword(userDN, "Moniki911", true);

				adq.addUserToGroup(userDN, adq.findDNbyCN("Group USER Everest"));
				adq.addUserToGroup(userDN, adq.findDNbyCN("Group USER disable double logon"));
				adq.addUserToGroup(userDN, adq.findDNbyCN("Group USER Moniki Wallpaper"));
			}

			System.out.println(userDN);
		}

		adc.close();
	}

	public static void main(String[] args) {
		CreateADUsersFromFile ms = new CreateADUsersFromFile();
		ms.userCreater();
	}

}
